﻿//*****************************************************************************
// (C) Copyright 2010 Tiago Pascoal
// This code is licensed under the Microsoft Public License (Ms-PL) 
// [http://www.microsoft.com/opensource/licenses.mspx#Ms-PL].
//
//*****************************************************************************
using System.Windows;

using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFhisto.UI.Controls.QueryEditorControl
{
    /// <summary>
    /// Interaction logic for QueryEditor.xaml
    /// </summary>
    public partial class QueryEditor : Window
    {
        QueryEditorControl _queryEditorControl;

        /// <summary>
        /// Predicate that indicates if the window should be automatically closed when the user asks for the query to be executed (run)
        /// </summary>
        public bool CloseOnRun { get; set; }
        
        public delegate void QueryRunHandler(object sender, QueryRunEventArgs e);

        /// <summary>
        /// Event that will be called when the user presses the Run query option
        /// </summary>
        public event QueryRunHandler QueryRun;


        /// <summary>
        /// Initializes the editor edit any query.
        /// 
        /// The TeamProject =  @project will be automaticallly added to the editor
        /// </summary>
        /// <param name="store"></param>
        /// <param name="teamProjectName"></param>
        public QueryEditor(WorkItemStore store, string teamProjectName) : this(store,teamProjectName,null)
        {
        }

        /// <summary>
        /// Initializes the editor with the specified query
        /// </summary>
        /// <param name="store"></param>
        /// <param name="teamProjectName"></param>
        /// <param name="wiql"></param>
        public QueryEditor(WorkItemStore store,string teamProjectName,string wiql)
        {
            InitializeComponent();

            _queryEditorControl = new QueryEditorControl(store, teamProjectName, wiql);

            _queryEditorControl.QueryRunRequested += new QueryEditorControl.QueryRunRequestedHandler(queryEditorControl_QueryRunRequested);

            winFormHost.Child = _queryEditorControl;
            
        }

        void queryEditorControl_QueryRunRequested(object sender, QueryRunEventArgs e)
        {
            if (QueryRun != null) QueryRun(this, e);
            
            if (CloseOnRun)this.Close();
        }

        
    }
}
